<?php

namespace app\admin\controller;

use think\Lang;
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use think\Db;
use think\Validate;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Advtype extends AdminControl {

    public function _initialize() {
        parent::_initialize();
        Lang::load(APP_PATH . 'admin/lang/'.config('default_lang').'/adv.lang.php');
    }
  
    #上传视频
    public function ajax_videos()
    {
        $old_name = $_FILES['file']['name'];
        $file_type = array('0' => 'mov', '1' => 'mp4', '2' => 'wmv', '3' => 'avi');
        $file_foot_name = substr($_FILES['file']['name'], (strripos($_FILES['file']['name'], ".")) + 1);
        if (!in_array($file_foot_name, $file_type)) {
            $data['msg'] = 1;
            return $data;
        }
        $upload_info = upload_qiniu('file');
        $data['msg'] = 1;
        $data['files_name'] = $upload_info['data'];
        return $data;
    }

    /**
     *
     * 管理广告类型
     */
    public function ap_manage() {
        $adv_model = model('adv');
        /**
         * 多选删除广告位
         */
        if (!request()->isPost()) {
            /**
             * 显示广告位管理界面
             */
            $condition = array();
            $orderby = '';
            $search_name = trim(input('get.search_name'));
            switch ($search_name) {
                case '视频广告':
                    $search_name = 1;
                    break;
                case '商品促销':
                    $search_name = 2;
                    break;
                case '项目定制':
                    $search_name = 3;
                    break;
                case '文章类':
                    $search_name = 4;
                    break;
                default:
                    $search_name = 0;
                    break;
            }
            if ($search_name != '') {
                $condition['ap_name'] = array('like', "%" . $search_name . "%");
            }
            $ap_list = $adv_model->getAdvpositionList($condition, '10', $orderby);
            $adv_list = $adv_model->getAdvList();
            $this->assign('ap_list', $ap_list);
            $this->assign('adv_list', $adv_list);
            $this->assign('showpage', $adv_model->page_info->render());

            $this->assign('filtered', $condition ? 1 : 0); //是否有查询条件

            $this->setAdminCurItem('ap_manage');
            return $this->fetch('ap_manage');
        }
    }

    /**
     *
     * 修改广告类型
     */
    public function ap_edit() {
        $ap_id = intval(input('param.ap_id'));
        $adv_model = model('adv');
        if (!request()->isPost()) {
            $condition['ap_id'] = $ap_id;
            $ap = $adv_model->getOneAdvposition($condition);
            $this->assign('ref_url', get_referer());
            $this->assign('ap', $ap);
            $this->assign('type', $ap['ap_name']);
            //获取所有项目定制
            $project_title =  db('goodsproject')->select();
            $this->assign('project_title', $project_title);
            //获取所有的文章
            $article_info = db('article')->select();
            $this->assign('article_info', $article_info);

            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);

            return $this->fetch('ap_edit');
        } else {
            $param['ap_id'] = $ap_id;
            $param['ap_name'] = trim(input('post.ap_name'));
            $param['ap_intro'] = trim(input('post.ap_intro'));
            $param['ap_width'] = intval(trim(input('post.ap_width')));
            $param['ap_height'] = intval(trim(input('post.ap_height')));
            //修改视频
            if (isset($_POST['videos']) && !empty($_POST['videos'])) {
                $param['videos'] = $_POST['videos'];
            }

            $param['goods_type'] = intval(input('post.goods_type'));
            $param['project_type'] = intval(input('post.project_type'));
            $param['article_type'] = intval(input('post.article_type'));

            if (input('post.ap_isuse') != '') {
                $param['ap_isuse'] = intval(input('post.ap_isuse'));
            }

//            $adv_validate = validate('adv');
//            if (!$adv_validate->scene('ap_edit')->check($param)) {
//                $this->error($adv_validate->getError());
//            }

            $result = $adv_model->editAdvposition($param);

            if ($result>=0) {
                $this->log(lang('ap_change_succ') . '[' . input('post.ap_name') . ']', null);
                dsLayerOpenSuccess('修改成功');
            } else {
                $this->error('修改失败');
            }
        }
    }
    /**
     *
     * 新增广告类型
     */
    public function ap_add() {
        if (!request()->isPost()) {
            $ap['ap_isuse'] = 1;
            $this->assign('ap', $ap);
            //获取所有项目定制
            $project_title =  db('goodsproject')->select();
            $this->assign('project_title', $project_title);
            //获取所有的文章
            $article_info = db('article')->select();
            $this->assign('article_info', $article_info);
            return $this->fetch('ap_form');
        } else {
            $adv_model = model('adv');
            $insert_array = array();
            //添加视频
            if (isset($_POST['videos']) && !empty($_POST['videos'])) {
                $insert_array['videos'] = $_POST['videos'];
            }

            $insert_array['ap_name'] = trim(input('post.ap_name'));
            $insert_array['ap_intro'] = trim(input('post.ap_intro'));
            $insert_array['ap_isuse'] = intval(input('post.ap_isuse'));
            $insert_array['ap_width'] = intval(input('post.ap_width'));
            $insert_array['ap_height'] = intval(input('post.ap_height'));

            $insert_array['goods_type'] = intval(input('post.goods_type'));
            $insert_array['project_type'] = intval(input('post.project_type'));
            $insert_array['article_type'] = intval(input('post.article_type'));
            $insert_array['time'] = time();

            $adv_validate = validate('adv');
            if (!$adv_validate->scene('ap_add')->check($insert_array)) {
                $this->error($adv_validate->getError());
            }

            $result = $adv_model->addAdvposition($insert_array);

            if ($result) {
                $this->log(lang('ap_add_succ') . '[' . input('post.ap_name') . ']', null);
                dsLayerOpenSuccess(lang('ap_add_succ'));
            } else {
                $this->error(lang('ap_add_fail'));
            }
        }
    }

    /**
     *
     * 删除广告类型
     */
    public function ap_del() {
        $adv_model = model('adv');
        /**
         * 删除一个广告类型
         */
        $ap_id = intval(input('param.ap_id'));
        $result = $adv_model->delAdvposition($ap_id);

        if (!$result) {
            ds_json_encode('10001', '删除失败');
        } else {
            $this->log(lang('ap_del_succ') . '[' . $ap_id . ']', null);
            ds_json_encode('10000', '删除成功');
        }
    }

    //广告位管理
    public  function adv_positiomn()
    {
        $adv_model = model('adv');
        /**
         * 多选删除广告位
         */
        if (!request()->isPost()) {
            /**
             * 显示广告位管理界面
             */
            $condition = array();
            $orderby = '';
            $search_name = trim(input('get.search_name'));
            switch ($search_name) {
                case '视频广告':
                    $search_name = 1;
                    break;
                case '商品促销':
                    $search_name = 2;
                    break;
                case '项目定制':
                    $search_name = 3;
                    break;
                case '文章类':
                    $search_name = 4;
                    break;
                default:
                    $search_name = 0;
                    break;
            }
            if ($search_name != '') {
                $condition['ap_name'] = array('like', "%" . $search_name . "%");
            }
            $ap_list = $adv_model->getAdvpositionList($condition, '10', $orderby);
            $adv_list = $adv_model->getAdvList();
            $this->assign('ap_list', $ap_list);
            $this->assign('adv_list', $adv_list);
            $this->assign('showpage', $adv_model->page_info->render());

            $this->assign('filtered', $condition ? 1 : 0); //是否有查询条件

            //查询所有的广告类型
            $type = db('advposition')->select();
            $this->assign('type', $type);
            $this->setAdminCurItem('adv_positiomn');
            return $this->fetch('adv_positiomn');
        }

    }




    /**
     *
     * 广告管理
     */
    public function adv() {
        $adv_model = model('adv');

        $ap_id = intval(input('param.ap_id'));
        if (!request()->isPost()) {
            $condition = array();
            if ($ap_id) {
                $condition['ap_id'] = $ap_id;
            }
            $adv_info = $adv_model->getAdvList($condition, 20, '', '');
            $this->assign('adv_info', $adv_info);
            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            if ($ap_id) {
                $ap_condition=array();
                $ap_condition['ap_id'] = $ap_id;
                $ap = $adv_model->getOneAdvposition($ap_condition);
                $this->assign('ap_name', $ap['ap_name']);
            } else {
                $this->assign('ap_name', '');
            }

            $this->assign('show_page', $adv_model->page_info->render());

            $this->assign('filtered', $condition ? 1 : 0); //是否有查询条件
            $this->setAdminCurItem('adv');
            return $this->fetch('adv_index');
        }
    }

    /**
     * 管理员添加广告
     */
    public function adv_add() {
        $adv_model = model('adv');
        if (!request()->isPost()) {
            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            $adv = array(
                'ap_id' => 0,
                'adv_enabled' => '1',
                'adv_startdate' => time(),
                'adv_enddate' => time() + 24 * 3600 * 365,
            );
            $this->assign('adv', $adv);
            return $this->fetch('adv_form');
        } else {
            $insert_array['ap_id'] = intval(input('post.ap_id'));
            $insert_array['adv_title'] = trim(input('post.adv_name'));
//            $insert_array['adv_link'] = input('post.adv_link');
//            $insert_array['adv_bgcolor'] = input('post.adv_bgcolor');

            $insert_array['adv_sort'] = input('post.adv_sort');
            $insert_array['adv_enabled'] = input('post.adv_enabled');
            $insert_array['adv_startdate'] = $this->getunixtime(input('post.adv_startdate'));
            $insert_array['adv_enddate'] = $this->getunixtime(input('post.adv_enddate'));

            //上传文件保存路径
            $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_ADV;
            if (!empty($_FILES['adv_code']['name'])) {
                $file = request()->file('adv_code');
                $info = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
                if ($info) {
                    $insert_array['adv_code'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }
            $adv_validate = validate('adv');
            if (!$adv_validate->scene('adv_add')->check($insert_array)) {
                $this->error($adv_validate->getError());
            }

            //广告信息入库
            $result = $adv_model->addAdv($insert_array);

            if ($result) {
                $this->log(lang('adv_add_succ') . '[' . input('post.adv_name') . ']', null);
                dsLayerOpenSuccess(lang('adv_add_succ'));
//                $this->success(lang('adv_add_succ'), url('Adv/adv', ['ap_id' => input('post.ap_id')]));
            } else {
                $this->error(lang('adv_add_fail'));
            }
        }
    }

    /**
     *
     * 修改广告
     */
    public function adv_edit() {
        $adv_id = intval(input('param.adv_id'));
        $adv_model = model('adv');
        //获取指定广告
        $condition['adv_id'] = $adv_id;
        $adv = $adv_model->getOneAdv($condition);
        if (!request()->isPost()) {
            //获取广告列表
            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            $this->assign('adv', $adv);
            $this->assign('ref_url', get_referer());
            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);
            return $this->fetch('adv_form');
        } else {
            $param['adv_id'] = $adv_id;
            $param['ap_id'] = intval(input('post.ap_id'));
            $param['adv_title'] = trim(input('post.adv_name'));
            $param['adv_link'] = input('post.adv_link');
            $param['adv_bgcolor'] = input('post.adv_bgcolor');
            $param['adv_sort'] = input('post.adv_sort');
            $param['adv_enabled'] = input('post.adv_enabled');
            $param['adv_startdate'] = $this->getunixtime(trim(input('post.adv_startdate')));
            $param['adv_enddate'] = $this->getunixtime(trim(input('post.adv_enddate')));


            if (!empty($_FILES['adv_code']['name'])) {
                //上传文件保存路径
                $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_ADV;
                $file = request()->file('adv_code');
                $info = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
                if ($info) {
                    //还需删除原来图片
                    if (!empty($adv['adv_code'])) {
                        @unlink($upload_file . DS . $adv['adv_code']);
                    }
                    $param['adv_code'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            $adv_validate = validate('adv');
            if (!$adv_validate->scene('adv_edit')->check($param)) {
                $this->error($adv_validate->getError());
            }

            $result = $adv_model->editAdv($param);

            if ($result>=0) {
                $this->log(lang('adv_change_succ') . '[' . input('post.ap_name') . ']', null);
                dsLayerOpenSuccess('修改成功');
//               $this->success(lang('adv_change_succ'), input('post.ref_url'));
            } else {
                $this->error('修改失败');
            }
        }
    }

    /**
     *
     * 删除广告
     */
    public function adv_del() {
        $adv_model = model('adv');
        /**
         * 删除一个广告
         */
        $adv_id = intval(input('param.adv_id'));
        $result = $adv_model->delAdv($adv_id);

        if (!$result) {
            ds_json_encode('10001', '删除失败');
        } else {
            $this->log(lang('adv_del_succ') . '[' . $adv_id . ']', null);
            ds_json_encode('10000', '删除成功');
        }
    }

    //轮播图
    public function rotation_chart() {
        $adv_model = model('adv');

        $ap_id = intval(input('param.ap_id'));
        if (!request()->isPost()) {
            $condition = array();
            if ($ap_id) {
                $condition['ap_id'] = $ap_id;
            }

//            $adv_info = $adv_model->getAdvList($condition, 20, '', '');
            $adv_info = db('advrotation')->where($condition)->select();
            $this->assign('adv_info', $adv_info);

            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            if ($ap_id) {
                $ap_condition=array();
                $ap_condition['ap_id'] = $ap_id;
                $ap = $adv_model->getOneAdvposition($ap_condition);
                $this->assign('ap_name', $ap['ap_name']);
            } else {
                $this->assign('ap_name', '');
            }

//            $this->assign('show_page', $adv_model->page_info->render());

            $this->assign('filtered', $condition ? 1 : 0); //是否有查询条件
            $this->setAdminCurItem('rotation_chart');
            return $this->fetch('rotation_chart');
        }
    }
    /**
     *
     * 新增轮播图
     */
    public function rotation_add() {
        $adv_model = model('adv');
        if (!request()->isPost()) {
            $ap['ap_isuse'] = 1;
            $this->assign('ap', $ap);
            //获取所有项目定制
            $project_title =  db('goodsproject')->select();
            $this->assign('project_title', $project_title);
            //获取所有的文章
            $article_info = db('article')->select();
            $this->assign('article_info', $article_info);

            //获取所有分类
            $ap_type = db('advposition')->select();
            $this->assign('ap_type', $ap_type);
            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            return $this->fetch('rotation_add');
        } else {
            $adv_model = model('adv');
            $insert_array = array();
            //添加视频
            if (isset($_POST['videos']) && !empty($_POST['videos'])) {
                $insert_array['videos'] = $_POST['videos'];
            }
            //上传文件保存路径
            $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_ADV;
            if (!empty($_FILES['adv_code']['name'])) {
                $file = request()->file('adv_code');
                $info = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
                if ($info) {
                    $insert_array['img_code'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

            $insert_array['name'] = trim(input('post.adv_name'));
            $insert_array['width'] = intval(input('post.ap_width'));
            $insert_array['type'] = intval(input('post.type'));
            $insert_array['height'] = intval(input('post.ap_height'));
            $insert_array['ap_id'] = intval(input('post.ap_id'));
            $insert_array['time'] = time();

            $result = db('advrotation')->insertGetId($insert_array);

            if ($result) {
                $this->log(lang('ap_add_succ') . '[' . input('post.ap_name') . ']', null);
                dsLayerOpenSuccess(lang('ap_add_succ'));
            } else {
                $this->error(lang('ap_add_fail'));
            }
        }
    }
    /**
     *
     * 修改轮播图
     */
    public function rotation_edit() {
        $adv_id = intval(input('param.adv_id'));
        $adv_model = model('adv');
        //获取指定广告
        $condition['id'] = $adv_id;
        $adv = db('advrotation')->where($condition)->find();
//        $adv = $adv_model->getOneAdv($condition);
        if (!request()->isPost()) {
            //获取广告列表
            $ap_list = $adv_model->getAdvpositionList();
            $this->assign('ap_list', $ap_list);
            $this->assign('adv', $adv);
            $this->assign('ref_url', get_referer());
            $config = db('config')->where('code', 'qiniu_http')->find();
            $this->assign('http_name', $config['value']);
            return $this->fetch('rotation_edit');
        } else {
            $rotation_id = input('post.rotation_id');

            $param['name'] = trim(input('post.adv_name'));
            $param['width'] = intval(input('post.ap_width'));
            $param['type'] = intval(input('post.type'));
            $param['height'] = intval(input('post.ap_height'));
//            $param['adv_startdate'] = $this->getunixtime(trim(input('post.adv_startdate')));
//            $param['adv_enddate'] = $this->getunixtime(trim(input('post.adv_enddate')));
            if (isset($_POST['videos']) && !empty($_POST['videos'])) {
                $param['videos'] = $_POST['videos'];
            }
            if (!empty($_FILES['adv_code']['name'])) {
                //上传文件保存路径
                $upload_file = BASE_UPLOAD_PATH . DS . ATTACH_ADV;
                $file = request()->file('adv_code');
                $info = $file->rule('uniqid')->validate(['ext' => ALLOW_IMG_EXT])->move($upload_file);
                if ($info) {
                    //还需删除原来图片
                    if (!empty($adv['adv_code'])) {
                        @unlink($upload_file . DS . $adv['adv_code']);
                    }
                    $param['img_code'] = $info->getFilename();
                } else {
                    // 上传失败获取错误信息
                    $this->error($file->getError());
                }
            }

//            $adv_validate = validate('adv');
//            if (!$adv_validate->scene('adv_edit')->check($param)) {
//                $this->error($adv_validate->getError());
//            }
//            $result = $adv_model->editAdv($param);
            $result = db('advrotation')->where(['id'=>$rotation_id])->update($param);
            if ($result>=0) {
                $this->log(lang('adv_change_succ') . '[' . input('post.ap_name') . ']', null);
                dsLayerOpenSuccess('修改成功');
//               $this->success(lang('adv_change_succ'), input('post.ref_url'));
            } else {
                $this->error('修改失败');
            }
        }
    }

    /**
     *
     * 删除广告类型
     */
    public function rotation_del() {
        $adv_model = model('adv');
        /**
         * 删除一个广告类型
         */
        $ap_id = intval(input('param.adv_id'));
        $result  = db('advrotation')->where(['id'=>$ap_id])->delete();
        if (!$result) {
            ds_json_encode('10001', '删除失败');
        } else {
            $this->log(lang('ap_del_succ') . '[' . $ap_id . ']', null);
            ds_json_encode('10000', '删除成功');
        }
    }
    /**
     *
     * 获取UNIX时间戳
     */
    public function getunixtime($time) {
        $array = explode("-", $time);
        $unix_time = mktime(0, 0, 0, $array[1], $array[2], $array[0]);
        return $unix_time;
    }

    public function ajax() {
        $adv_model = model('adv');
        switch (input('get.branch')) {
            case 'ap_branch':
                $column = trim(input('param.column'));
                $value = trim(input('param.value'));
                $adv_id = intval(input('param.id'));
                $param['ap_id'] = $adv_id;
                $param[$column] = trim($value);
                $result = $adv_model->editAdvposition($param);
                break;
            //ADV数据表更新
            case 'adv_branch':
                $column = trim(input('param.column'));
                $value = trim(input('param.value'));
                $adv_id = intval(input('param.id'));
                $param[$column] = trim($value);
                $result = $adv_model->editAdv(array_merge($param, array('adv_id' => $adv_id)));
                break;
        }
        if($result>=0){
            echo 'true';
        }else{
            echo false;
        }
    }

    function adv_template() {
        $pages = $this->_get_editable_pages();
        $this->assign('pages', $pages);
        $this->setAdminCurItem('adv_template');
        return $this->fetch();
    }

    /**
     *    获取可以编辑的页面列表
     */
    function _get_editable_pages() {
        return array(
            lang('homepage') => url('Home/Index/index',['edit_ad'=>1]),
            lang('flea') => url('Home/Flea/index',['edit_ad'=>1]),
        );
    }

    /**
     * 获取卖家栏目列表,针对控制器下的栏目
     */
    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'ap_manage',
                'text' => '广告类型',
                'url' => url('Adv/ap_manage')
            ),
        );
        $menu_array[] = array(
            'name' => 'ap_add',
            'text' => '新增广告类型',
            'url' =>"javascript:dsLayerOpen('".url('Adv/ap_add')."','".'新增广告类型'."','900px','585px')"
        );
//        $menu_array[] = array(
//            'name' => 'adv_positiomn',
//            'text' => '广告位管理',
//            'url' => url('Adv/adv_positiomn')
//        );
//        $menu_array[] = array(
//            'name' => 'adv',
//            'text' => lang('adv_manage'),
//            'url' => url('Adv/adv')
//        );
//        $menu_array[] = array(
//            'name' => 'adv_add',
//            'text' => lang('adv_add'),
//            'url' => "javascript:dsLayerOpen('".url('Adv/adv_add', ['ap_id' => input('param.ap_id')])."','".lang('adv_add')."','900px','700px')"
//        );
//        $menu_array[] = array(
//            'name' => 'adv_template',
//            'text' => lang('adv_template'),
//            'url' => url('Adv/adv_template')
//        );
//        $menu_array[] = array(
//            'name' => 'rotation_chart',
//            'text' => '轮播图',
//            'url' => url('Adv/rotation_chart')
//        );
//        $menu_array[] = array(
//            'name' => 'rotation_add',
//            'text' => '新增轮播图',
//            'url' => "javascript:dsLayerOpen('".url('Adv/rotation_add')."','".'新增轮播图'."')"
//        );
        return $menu_array;
    }

}

?>
